Test Series - Data Structure

Test Number 36/115

Q: What is a sparse array?
A. Data structure for representing arrays of records
B. Data structure that compactly stores bits
C. An array in which most of the elements have the same value
D. An array in which memory is allocated in run time
Solution: They are set to a default value, usually 0 or null.
Q: When do you use a sparse array?
A. When there are unique elements in the array
B. When the array has more occurrence of zero elements
C. When the data type of elements differ
D. When elements are sorted
Solution: It need not necessarily be zero, it could be any default value, usually zero or null.
Q: What is the difference between a normal(naive) array and a sparse array?
A. Sparse array can hold more elements than a normal array
B. Sparse array is memory efficient
C. Sparse array is dynamic
D. A naive array is more efficient
Solution: A naive implementation allocates space for the entire size of the array, whereas a sparse array(linked list implementation) allocates space only for the non-default values.
Q:  Choose the code which performs the store operation in a sparse array.(Linked list implementation)
A. public void store(int index, Object val) { List cur = this; List prev = null; List node = new List(index); node.val = val; while (cur != null && cur.index < index) { prev = cur; cur = cur.next; } if (cur == null) { prev.next = node; } else { if (cur.index == index) { System.out.println("DUPLICATE"); return; }
B. public void store(int index, Object val) { List cur = this; List prev = null; List node = new List(index); node.val = val; while (prev != null && prev.index < index) { prev = cur; cur = cur.next; } if (cur == null) { prev.next = node; } else { if (cur.index == index) { System.out.println("DUPLICATE"); return;
C. public void store(int index, Object val) { List cur = this; List prev = null; List node = new List(index); node.val = val; while (cur != null && cur.index < index) { cur = cur.next; prev = cur; } if (cur == null) { prev.next = node; } else { if (cur.index == index) { System.out.println("DUPLICATE"); return; }
D. public void store(int index, Object val) { List cur = this; List prev = null; List node = new List(index); node.val = val; while (cur != null && prev.index < index) { cur = cur.next; prev = cur; } if (cur == null) { prev.next = node; } else { if (cur.index == index) { System.out.println("DUPLICATE"); return; } prev.next = cur; node.next = node; } return; }
Solution: Create a new node and traverse through the list until you reach the correct position, check for duplicate and nullity of the list and then insert the node.
Q: Which of the following performs the fetch operation?
A. public Object fetch(int index) { List cur = this.next; Object val = null; while (cur != null && cur.index != index) { cur = cur.next; } if (cur != null) { val = cur.val; } else { val = null; } return val; }
B. public Object fetch(int index) { List cur = this; Object val = null; while (cur != null && cur.index != index) { cur = cur.next; } if (cur != null) { val = cur.val; } else { val = null; } return val; }
C. public Object fetch(int index) { List cur = this; Object val = null; while (cur != null && cur.index != index) { cur = cur.index; } if (cur != null) { val = cur.val; } else { val = null; } return val; }
D. public Object fetch(int index) { List cur = this.next; Object val = null; while (cur != null && cur.index != index) { cur = cur.index; } if (cur != null) { val = cur.val; } else { val = null; } return val; }
Solution: You travers through the array until the end is reached or the index is found and return the element at that index, null otherwise.
Q: Choose the appropriate code that counts the number of non-zero(non-null) elements in the sparse array.
A. public int count() { int count = 0; for (List cur = this.next; (cur != null); cur = cur.next) { count++; } return count; }
B. public int count() { int count = 0; for (List cur = this; (cur != null); cur = cur.next) { count++; } return count; }
C. public int count() { int count = 1; for (List cur = this.next; (cur != null); cur = cur.next) { count++; } return count; }
D. public int count() { int count = 1; for (List cur = this.next; (cur != null); cur = cur.next.next) { count++; } return count; }
Solution: A simple ‘for loop’ to count the non-null elements.
Q: Suppose the contents of an array A are, A = {1, null, null, null, null, 10};
What would be the size of the array considering it as a normal array and a sparse array?
A. 6 and 6
B. 6 and 2
C. 2 and 6
D. 2 and 2
Solution: A normal array considers null also as an element, but in the sparse array only a non-zero or a non-null element is considered.
Q:  What is sparsity of a matrix?
A. The fraction of zero elements over the total number of elements
B. The fraction of non-zero elements over the total number of elements
C. The fraction of total number of elements over the zero elements
D. The fraction of total number of elements over the non-zero elements
Solution: Sparsity of a matrix is the fraction of number of zero elements over the total number of zero elements.
Q: How would you store an element in a sparse matrix?
A. public void store(int row_index, int col_index, Object val) { if (row_index < 0 || row_index > N) { System.out.println("row index out of bounds"); return; } if (col_index < 0 || col+index > N) { System.out.println("column index out of bounds"); return; } sparse_array[row_index].store(col_index, val); }
B. public void store(int row_index, int col_index, Object val) { if (row_index < 0 || row_index > N) { System.out.println("column index out of bounds"); return; } if (col_index < 0 || col+index > N) { System.out.println("row index out of bounds"); return; } sparse_array[row_index].store(col_index, val); }
C. public void store(int row_index, int col_index, Object val) { if (row_index < 0 && row_index > N) { System.out.println("row index out of bounds"); return; } if (col_index < 0 && col+index > N) { System.out.println("column index out of bounds"); return; } sparse_array[row_index].store(col_index, val); }
D. public void store(int row_index, int col_index, Object val) { if (row_index < 0 && row_index > N) { System.out.println("column index out of bounds"); return; } if (col_index < 0 && col+index > N) { System.out.println("row index out of bounds"); return; } sparse_array[row_index].store(col_index, val); }
Solution: Each row in a sparse matrix acts as a sparse array, hence this row with the specified col_index is the array and the specified position where the element is stored.
Q: What is the functionality of the following piece of code?

public Object function(int row_index, int col_index)
{
        if (row_index < 0 || col_index > N)
	{
            System.out.println("column index out of bounds");
			return;
	}
        return (sparse_array[row_index].fetch(col_index));
}
A. Store the element in the specified position
B. Get the element from the specified position
C. Alter the element in the specified position
D. Removes the element from the specified position
Solution: The fetch method of SparseArray class is called , the row specified by row_index makes it an array and the col_index denotes the specified position.

You Have Score    /10